// store/index.ts
import { createStore } from 'vuex'
import Logs from '@/utils/logs'
import bus from '@/utils/bus'

const store = createStore({
  state() {
    return {
      count: 0,
      userInfo: {
        userId: localStorage.getItem('userId')
      }, // 用户信息
      logs: new Logs() // 操作日志
    }
  },
  mutations: {
    increment(state: any) {
      state.count++
    },
    setUserInfo(state, data: any) {
      state.userInfo = data
    },
    // 追加日志
    addLog(state: any, val: string) {
      state.logs.add(val)
      console.log(state.logs)
    },
    nextLog(state: any) {
      bus.emit('logChange', state.logs.next())
    },
    prevLog(state: any) {
      bus.emit('logChange', state.logs.prev())
    }
  }
})

export default store
